﻿using Furion;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Mrhd.Core.Entities;
using Mrhd.Core.Extensions;
using System;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Mrhd.Application
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserAppService : IDynamicApiController
    {
        private readonly IUserService _service;
        public UserAppService(IUserService userService)
        {
            _service = userService;
        }

        /// <summary>
        /// 获取当前登录用户信息
        /// </summary>
        /// <returns></returns>
        public object GetUserinfo()
        {
            var id = App.User?.FindFirstValue("id");
            var username = App.User?.FindFirstValue("username");
            var name = App.User?.FindFirstValue("name");
            return new { id, username, name };
        }

        /// <summary>
        /// 创建网站用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<UserEntityDto> Create(UserEntityDto user)
        {
            var entity = await _service.CreateAsync(user.Adapt<UserEntity>());
            return entity.Adapt<UserEntityDto>();
        }

        /// <summary>
        /// 获取用户可访问的菜单
        /// </summary>
        /// <returns></returns>
        public object GetMenus()
        {
            var isAdmin = App.User.FindFirstValue("isAdmin");
            if (isAdmin == "1") return new { all = true };
            throw Oops.Oh("未实现菜单权限功能");
        }

    }

}
